module NinjaWebDebugger
  
  # Request variables
  @@log_key = nil
  @@request_id = nil
  @@db_calls = 0
  
  # Logs variables
  @@logs = {}
  @@request_cnt = 0

  
    # Create a log entry as part of the before filter controller action
    def self.before(log_key=:tmp, session=nil, request=nil)
      # Set the log key as Module variable so we can access it from the db logger
      @@log_key = log_key
      @@request_id = request_id
      
      @@logs[log_key] ||= {}
      
      current_log = @@logs[log_key][(@@request_id || :tmp)] ||= {}
      current_log[:db_logs] ||= {}
      current_log[:request_timing] ||= {}
      
      current_log[:request_timing][:start] ||= Time.now
      current_log[:request_data] = request.env
      current_log[:session_data] = session.data
      current_log[:db_logs] ||= {}
      
      # We return the request_id so the view knows its request id and use it for a later Ajax call
      return self.current_request_id
    end
    
    # Update a log as part of the after filter controller action
    def self.after(log_key=:tmp, request_id=nil)
      increase_log_count
      current_log = @@logs[log_key][request_id]
      current_log[:request_timing][:end] = Time.now
      current_log[:request_timing][:duration] = query_duration(log_key, request_id)
      
      reset_keys
      
      unless @@logs[:tmp].nil? || @@logs[:tmp][:tmp].nil? || @@logs[:tmp][:tmp][:db_logs].nil?
        @@logs[:tmp][:tmp][:db_logs].each_pair do |key, log|
          @@logs[log_key][request_id][:db_logs][key] = log
        end
        @@logs[:tmp][:tmp][:db_logs] = nil
      end
      @@logs[log_key][request_id][:db_total_calls] = @@logs[log_key][request_id][:db_logs].size
      
      # TODO: Dump to DB if the option was set
      
      # Reset after 300 requests
      if @@logs.size > 300 
        @@logs = {}
      end
      reset_keys # not the logs!
    end
    
    # Return a Logs hash for the log key, request_id passed
    def self.get_logs(key=nil, id=nil)
      @@logs[key][id]
    end
    
    # Return the curent request id or :tmp if the request id isn't ready yet
    def self.current_request_id
      @@request_id || :tmp
    end
    
    # Return the curent log key or :tmp if the request log key isn't ready yet
    def self.current_log_key
      @@log_key || :tmp
    end
    
    # DB calls order
    def self.current_db_calls
      @@db_calls || 0
    end
    
    # Add a new DB log, since DB logs might be saved before the request log is initiated we need to create a temp request log to store the data
    def self.new_db_log(log=nil)
      temp
      @@logs[current_log_key][current_request_id][:db_logs][current_db_calls] = log
      increase_db_calls
    end
    
    # Define the plugin routes
    # This needs to be added to you routes.rb file
    def self.routes
      ActionController::Routing::Routes.add_route "/ninja_web_debugger/:action", :controller => "ninja_web_debugger"
    end
    
    
    private


    # never gets reset (unlike current_request_id), used to create a new log
    def self.request_id
      @@request_cnt
    end
    
    def self.increase_db_calls
      @@db_calls += 1
    end
    
    def self.increase_log_count
      @@request_cnt += 1
    end
    
    # Calculate the query duration
    def self.query_duration(key=nil, id=nil)
     @@logs[key][id][:request_timing][:end] - @@logs[key][id][:request_timing][:start]
    end
    
    # Reset the keys between requests
    def self.reset_keys
      @@log_key = nil
      @@request_id = nil
      @@db_calls = 0
    end
    
    # Create temp Hashes in case we are starting logging before the before filter was triggered
    def self.temp
      @@logs[NinjaWebDebugger.current_log_key] = {} if @@logs[NinjaWebDebugger.current_log_key].nil?
      @@logs[NinjaWebDebugger.current_log_key][NinjaWebDebugger.current_request_id] = {} if @@logs[NinjaWebDebugger.current_log_key][NinjaWebDebugger.current_request_id].nil?
      @@logs[NinjaWebDebugger.current_log_key][NinjaWebDebugger.current_request_id][:db_logs] = {} if @@logs[NinjaWebDebugger.current_log_key][NinjaWebDebugger.current_request_id][:db_logs].nil?      
    end
  
end